package com.ruoyi.match.controller;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.match.domain.ChMarket;
import com.ruoyi.match.service.ChMarketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: AnYu
 * @Version: v1.00
 * @Created: 2023/12/20 19:40
 */
@RestController
@RequestMapping("/kLine")
public class KLineChartController {

    @Autowired
    private ChMarketService chMarketService;

    @RequiresPermissions("match:kLine:getData")
    @GetMapping("/getData/{gid}")
    public Map<String, Object> kWeek(@PathVariable String gid){
        Map<String, Object> resultMap = new HashMap<>();
        List<ChMarket> list = chMarketService.listKLine(gid, DateUtil.offsetDay(new Date(),-7),new Date());
        List<String> dateList = new ArrayList<>();
        List dataList = new ArrayList<>();
        list.stream().collect(Collectors.groupingBy(ChMarket::getDate)).forEach((date, ChMarket)->{
            Object[] objects = ChMarket.stream().map(chMarket -> new BigDecimal[]{
                    chMarket.getTodayStartPri(), chMarket.getNowPri(), chMarket.getTodayMin(), chMarket.getTodayMax()
            }).toArray();
            dateList.add(String.valueOf(date));
            dataList.add(objects.length>0?objects[0]:objects);
        });
        resultMap.put("date",dateList);
        resultMap.put("data",dataList);
        return resultMap;
    }


    @RequiresPermissions("match:kLine:getName")
    @GetMapping("/getName")
    public AjaxResult getName(){
        List<ChMarket> chMarkets = chMarketService.listGid();
        System.err.println(chMarkets);
       return AjaxResult.success(chMarketService.listGid());
    }

}
